<?php
// 本类由系统自动生成，仅供测试用途
class PlanAction extends Action {

	
	//预设  para一般自身的所有以及扩展的zabojingua东西
	//聚合
    private $all=array(
    	'mdmk'=>'Plan',//NB
  		'ttl'=>'生产计划',
  		'jn'=>array('tb_line ON f_plan_lineid=lineid','tb_item ON f_plan_itemid=itemid','tb_banci ON f_plan_banciid=banciid'),//NB
      //自己的全部+f的显示的东西
  		'para'=>array('planid'=>'planID','f_plan_lineid'=>'产线','linenm'=>'产线','f_plan_itemid'=>'项目','itemnm'=>'项目名称','itemcode'=>'项目编码','plantm'=>'时间','f_plan_banciid'=>'班次','bancinm'=>'班次','planvolume'=>'计划量','planvolumebox'=>'计划箱数','planworktm'=>'计划工作时间','planrealvolume'=>'实际量','planodr'=>'排序','planstat'=>'状态','planstarttm'=>'计划启动时间'),//NB
  		//抛去不是zabojin的属性针对para
      'notself'=>array('linenm','itemnm','itemcode','bancinm'),
       ##########modify 添加修改中不需要展示和理会的属性 针对para
      'no_update'=>array('planid','linenm','itemnm','itemcode','bancinm'),
      #####update的时候允许为空的值 针对zabojin刨掉不然显示的update字段后
      'allowempty'=>array(),

      'hide_fld'=>array('planid','f_plan_lineid','f_plan_itemid','f_plan_banciid'),//NB
      'hide_cdt'=>array('planid','linenm','itemnm','itemcode','bancinm'),//NB
  		
    //   'spccdtls'=>array('spccdt_0'=>array('planid<>0','planID不为0【废话只是测试】')),
  		// 'odrls'=>array('plannm'),
    //   'spccdt_dflt'=>array('spccdt_0'),
    //   'odr_dflt'=>array('plannm'=>'ASC'),

      'spccdtls'=>array(),//NB
      'odrls'=>array('plantm','f_plan_banciid','planodr'),//NB
      'spccdt_dflt'=>array(),//NB
      'odr_dflt'=>array('plantm'=>'DESC','f_plan_banciid'=>'DESC','planodr'=>'ASC'),//NB
      //hide的fld必须有，他们虽然不显示但是必须选择，这样才能在第一次进入query的时候，隐藏属性可以被调用，特别是id和fid
  		'fld_dflt'=>array('planid','f_plan_lineid','linenm','f_plan_itemid','itemnm','itemcode','plantm','f_plan_banciid','bancinm','planvolume','planvolumebox','planworktm','planrealvolume','planodr','planstat','planstarttm'),//NB
  		'cdt_dflt'=>array(),//NB
  		
  		'lmt_dflt'=>10,//NB
  		
  		'defaultls'=>1,//默认枚举//NB
  		##########view
  		'no_view'=>array('planid','f_plan_lineid','f_plan_itemid','f_plan_banciid'),
	   
      #########删除提醒
      'deleteconfirm'=>'确定要删除此条记录？',
      #####转义
      'transmean'=>array(),//NB
      #####默认值
      'dfltvalue'=>array('planrealvolume'=>'0','planstat'=>'0'),
      ########新增属性
      'linkage'=>array(),//如果itemc的变换需要转换成2015_itemc 就是2  否则如果是1 非2情况下联动没有值，不允许下拉，3联动不造成下拉有无
      ########这里是谁影响谁，用在AJAX联动 效果极佳
      'impactfactor'=>array(),
      
    );

    //公版
    public function query(){
    	header("Content-Type:text/html; charset=utf-8");
    	$pb=D('PB');
      $pb->query($this->all);
      $this->display('query');
  
    }
    
    //公版
    public function view(){
    	header("Content-Type:text/html; charset=utf-8");
    	$pb=D('PB');
    	$pb->view($this->all);
		  $this->display('Cmn:view');
    }
   
   	//公版
   	public function update(){
   		header("Content-Type:text/html; charset=utf-8");
    	$pb=D('PB');
    	$pb->update($this->all);
		  $this->display('update');
   	}

   	//公版
   	public function doupdate(){
   		header("Content-Type:text/html; charset=utf-8");
   		$pb=D('PB');$plan=D('Plan');$ps=D('Ps');

      //先获取原来的数据//似乎用不上...
      $planid=gp()['planid'];$itemid=gp()['f_plan_itemid'];
            

   		$arr_pattern=$pb->doupdate($this->all);
   		$data['pattern']=$arr_pattern['data'];

      //添加完plan后希望能够添加ps信息
      if($planid<>0&&$itemid){
        $ps->deletepslsbyplanid($planid);
        $arr_plano=$plan->getplanobyplanid($planid);$plano=$arr_plano['data'];
        $ps->add($plano['planid'],$plano['f_plan_lineid'],$plano['f_plan_itemid'],$plano['planvolumebox']);
      }
      if($planid==0){
        $arr_plano=$plan->getlastplano();$plano=$arr_plano['data'];
        $ps->add($plano['planid'],$plano['f_plan_lineid'],$plano['f_plan_itemid'],$plano['planvolumebox']);
      }
      
      

   		$this->ajaxReturn($data,'json');
   	}

   	//公版
   	public function dodelete(){
   		header("Content-Type:text/html; charset=utf-8");
   		$pb=D('PB');$ps=D('Ps');
   		$pb->dodelete($this->all);

      //然后删除此相关的peisong信息 ps
      $planid=gp()['id'];
      $ps->deletepslsbyplanid($planid);
      
  		
   		$this->ajaxReturn($data,'json');
   	}

    public function doclearplan(){
      header("Content-Type:text/html; charset=utf-8");
      $plan=M('plan');
      $plan->execute('TRUNCATE TABLE tb_plan');
      $ps=M('ps');
      $ps->execute('TRUNCATE TABLE tb_ps');
    
      $this->ajaxReturn($data,'json');
    }

    public function doautosetyuzhi(){
      header("Content-Type:text/html; charset=utf-8");
      $ps=M('ps');$yuzhi=M('Yuzhi');

        $ps->execute('TRUNCATE TABLE tb_yuzhi');
        $psls=$ps->select();
        foreach($psls as $psv){
            $dt['f_yuzhi_partid']=$psv['f_ps_partid'];
            $dt['f_yuzhi_lineid']=$psv['f_ps_lineid'];
            $dt['yuzhired']=20;
            $dt['yuzhiyellow']=40;
            $dt['yuzhispeed']=180;
            $dt['yuzhitaketime']=3;
            $yuzhi->data($dt)->add();
        }
        $this->ajaxReturn($data,'json');
    }

    public function dolinkage(){
      header("Content-Type:text/html; charset=utf-8");
      $nb=D('NB');
      $arr=$nb->dolinkage($this->all);
      
      $data=$arr['data'];
      
      $this->ajaxReturn($data,'json');
    }

    public function dogetitemlsbykw(){
      header("Content-Type:text/html; charset=utf-8");
      $item=D('Item');
      
      $kw=$_GET['kw'];
      $arr_itemls=$item->getitemlsbykw($kw);$itemls=$arr_itemls['data'];
      $data['itemls']=$itemls;
      
      $this->ajaxReturn($data,'json');
    }

    public function doqingkong(){
      header("Content-Type:text/html; charset=utf-8");
      $item=D('Item');
      
      $arr_itemls=$item->getitemlsbykw("");$itemls=$arr_itemls['data'];
      $data['itemls']=$itemls;
      
      $this->ajaxReturn($data,'json');
    }

    public function fenxi(){
        header("Content-Type:text/html; charset=utf-8");
        $item=D('Item');$part=D('Part');$parttype=D('Parttype');$exlchar=D('Exlchar');$line=D('Line');$item=D('Item');$plan=D('Plan');$ps=D('Ps');

        $all=$this->all;
        $para=$all['para'];
        
        import('@.ORG.PHPExcel');
        //设定缓存模式为经gzip压缩后存入cache（PHPExcel导入导出及大量数据导入缓存方式的修改 ）
        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
        $cacheSettings = array();
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);
    
        $objPHPExcel = new PHPExcel();
        // $con = mysql_connect("localhost","root","123456");
        // mysql_select_db('bms');
        //读入上传文件
        $myfl=session('myfl');$myfl=str_replace('/'.C('PROJECT'),'.',$myfl);
        //$objPHPExcel = PHPExcel_IOFactory::load($_FILES["inputExcel"]["tmp_name"]);
        $objPHPExcel = PHPExcel_IOFactory::load($myfl);
         
        $currentSheet = $objPHPExcel->getSheet(0);// 当前页
        $row_num = $currentSheet->getHighestRow();// 当前页行数
        $col_max = $currentSheet->getHighestColumn(); // 当前页最大列号

        $rslt='';$jieguo=1;

        
        //先找PM1标志
        $pm1_i=0;
        for($i = 2; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PM1'){
            $pm1_i=$i;
          }
        }
        
        //再找小立袋

        $dtj_i=0;
        for($i = 2; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='小立袋'){
            $dtj_i=$i;
          }
        }
        //找中间的备注
        $bz_mid_j=0;$bz_final_j=0;
        $i=$pm1_i-1;
        $j=2;$flag_beizhu=0;
        while(true){
          $j++;
          $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
          $ads=$char.$i;
          $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($cell=='备注'){
            $flag_beizhu++;
            if($flag_beizhu==1){$bz_mid_j=$j;}
            if($flag_beizhu==2){$bz_final_j=$j;break;}
          }
        }

        //先找PM2标志
        $pm2_i=0;
        for($i = $pm1_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PM2'){
            $pm2_i=$i;
          }
        }

        //先找PM3标志
        $pm3_i=0;
        for($i = $pm2_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PM3'){
            $pm3_i=$i;
          }
        }

        //先找PK1标志
        $pk1_i=0;
        for($i = $pm3_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PUCK1'){
            $pk1_i=$i;
          }
        }

        //先找PK2标志
        $pk2_i=0;
        for($i = $pk1_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PUCK2'){
            $pk2_i=$i;
          }
        }

        //先找PK3标志
        $pk3_i=0;
        for($i = $pk2_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PUCK3'){
            $pk3_i=$i;
          }
        }

         //先找PK4标志
        $pk4_i=0;
        for($i = $pk3_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='PUCK4'){
            $pk4_i=$i;
          }
        }

        //先找L1标志
        $l1_i=0;
        for($i = $pk4_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='Ronchi 1'){
            $l1_i=$i;
          }
        }

        //先找L2标志
        $l2_i=0;
        for($i = $l1_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='Ronchi2'){
            $l2_i=$i;
          }
        }

        //先找L3标志
        $l3_i=0;
        for($i = $l2_i+1; $i <= $row_num; $i++){
          $ads = 'B' . $i; // 单元格坐标address
          $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
          if($ctt=='Ronchi3'){
            $l3_i=$i;
          }
        }

        

        

        //获取时间
        //前一天的夜班  第二天的白班
        $ads = 'A1'; // 单元格坐标address
        $ctt=trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value

        $qianyitian=trim(explode('液体', explode('--', $ctt)[0])[1]);
        $nian=explode('/', $qianyitian)[0];
        $houyitian=$nian.'/'.trim(explode('生产', explode('--', $ctt)[1])[0]);
        $tm=strtotime($qianyitian);$qianyitian=date('Y-m-d',$tm);
        $tm=strtotime($houyitian);$houyitian=date('Y-m-d',$tm);


        for($i = $pm1_i; $i < $dtj_i; $i++){

          $yebanplano=array();$baibanplano=array();

          //确定好那条产线
          if($i<$pm2_i){$linenm='PM1';}else if($i<$pm3_i){$linenm='PM2';}else if($i<$pk1_i){$linenm='PM3';}else if($i<$pk2_i){$linenm='PK1';}else if($i<$pk3_i){$linenm='PK2';}else if($i<$pk4_i){$linenm='PK3';}else if($i<$l1_i){$linenm='PK4';}else if($i<$l2_i){$linenm='L1';}else if($i<$l3_i){$linenm='L2';}else if($i<$dtj_i){$linenm='L3';}

          $arr_lineo=$line->getlineobylinenm($linenm);$lineo=$arr_lineo['data'];
          
          
          for($j=3;$j<$bz_mid_j;$j++){
            
            if($j==3){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              //如果是数字就继续，否则肯定不对
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              if(is_numeric($cell)){
                $arr_itemo=$item->getitemobyitemcode($cell);$itemo=$arr_itemo['data'];
                if(!$itemo){$rslt=$rslt.$i.'行'.$j.'列的项目编号不存在，请检查<br>';$jieguo=2;break;}
                $yebanplano['f_plan_lineid']=$lineo['lineid'];
                $yebanplano['f_plan_itemid']=$itemo['itemid'];
                $yebanplano['plantm']=$qianyitian;
                $yebanplano['f_plan_banciid']=2;//B暂时代表夜班

              }else{
                //果断夜班没项目，看看白班
                break;
              }

            }else if($j==5){
              //既然能到4说明3已经通过了，有数字项目号
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $yebanplano['planvolume']=$cell;

            }else if($j==6){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $yebanplano['planvolumebox']=round($cell);
            }else if($j==7){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $yebanplano['planworktm']=$cell;
              $yebanplano['planrealvolume']=0;
              $arr_odr=$plan->getpreplanodr($yebanplano['plantm'],$yebanplano['f_plan_lineid'],$yebanplano['f_plan_banciid']);$odr=$arr_odr['data'];
              $yebanplano['planodr']=$odr+1;
              $yebanplano['planstat']=0;
              $arr_plano=$plan->add($yebanplano);$plano=$arr_plano['data'];
              //物料信息速度放进去
              $ps->add($plano['planid'],$lineo['lineid'],$itemo['itemid'],$yebanplano['planvolumebox']);
            }else{
              continue;
            }


          }

          for($j=$bz_mid_j+1;$j<$bz_final_j;$j++){
            
            if($j==$bz_mid_j+1){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              //如果是数字就继续，否则肯定不对
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              if(is_numeric($cell)){
                $arr_itemo=$item->getitemobyitemcode($cell);$itemo=$arr_itemo['data'];
                if(!$itemo){$rslt=$rslt.$i.'行'.$j.'列的项目编号不存在，请检查<br>';$jieguo=2;break;}
                $baibanplano['f_plan_lineid']=$lineo['lineid'];
                $baibanplano['f_plan_itemid']=$itemo['itemid'];
                $baibanplano['plantm']=$houyitian;
                $baibanplano['f_plan_banciid']=1;//A暂时代表白班

              }else{
                //果断夜班没项目，看看白班
                break;
              }

            }else if($j==$bz_mid_j+3){
              //既然能到此说明上一步已经通过了，有数字项目号
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $baibanplano['planvolume']=$cell;

            }else if($j==$bz_mid_j+4){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $baibanplano['planvolumebox']=$cell;
            }else if($j==$bz_mid_j+5){
              $arr_char=$exlchar->changechar($j);$char=$arr_char['data'];
              $ads = $char.$i; // 单元格坐标address
              $cell = trim($currentSheet->getCell($ads)->getFormattedValue());//cell_value
              $baibanplano['planworktm']=$cell;
              $baibanplano['planrealvolume']=0;
              $arr_odr=$plan->getpreplanodr($baibanplano['plantm'],$baibanplano['f_plan_lineid'],$baibanplano['f_plan_banciid']);$odr=$arr_odr['data'];
              $baibanplano['planodr']=$odr+1;
              $baibanplano['planstat']=0;
              $arr_plano=$plan->add($baibanplano);$plano=$arr_plano['data'];
              //物料信息速度放进去
              $ps->add($plano['planid'],$lineo['lineid'],$itemo['itemid'],$baibanplano['planvolumebox']);
            }else{
              continue;
            }


          }
  
         
        }
        
        $data['jieguo']=$jieguo;
        $data['rslt']=$rslt;

        $this->ajaxReturn($data,'json');
    }

}